package com.jwb.pub.serviceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.jwb.pub.dao.BaseDAO;
import com.jwb.pub.model.JavaModel;
import com.jwb.pub.service.ScheduleService;
import com.jwb.util.ToolsUtil;
@Service
public class ScheduleServiceImpl implements ScheduleService
{
	@Autowired
	private BaseDAO baseDAO;
	
	/**
	 * 添加任务
	 * @param jsdata
	 * @return
	 */
	public JSONObject scheduleAdd(JSONObject jsdata)
	{
		String id=!jsdata.containsKey("id") ? "" : jsdata.getString("id");
		String name=!jsdata.containsKey("name") ? "数据采集" : jsdata.getString("name");
		String url=!jsdata.containsKey("url") ? "" : jsdata.getString("url");
		String req_data=!jsdata.containsKey("req_data") ? "" : jsdata.getString("req_data");
		String sts=!jsdata.containsKey("sts") ? "" : jsdata.getString("sts");
		JSONObject jsobject = new JSONObject();
		jsobject.put("respCode", "-1");
		jsobject.put("respDesc", "数据异常");
	
		String sql="INSERT INTO SCHEDULE ( "+
					"  id,"+
					"  NAME,"+
					"  url,"+
					"  req_data,"+
					"  sts,"+
					"  sts_date"+
					" ) "+
					" VALUES"+
					"  ("+
					"    ?,"+
					"    ?,"+
					"    ?,"+
					"    ?,"+
					"    ?,"+
					"    now() )";
		ArrayList<JavaModel> list = new ArrayList<>();
		list.add(new JavaModel(id));
		list.add(new JavaModel(name));
		list.add(new JavaModel(url));
		list.add(new JavaModel(req_data));	
		list.add(new JavaModel(sts));
		boolean flag=baseDAO.excuteSql(sql, list);		
		if(flag)
		{
			jsobject.put("respCode", "0");
			jsobject.put("respDesc", "success");
			
		}
		return jsobject;
	}
	
	/**
	 * 更新状态
	 * @param jsdata
	 * @return
	 */
	public JSONObject scheduleUpateStatus(JSONObject jsdata)
	{
		
		String id=!jsdata.containsKey("id") ? "" : jsdata.getString("id");
		String sts=!jsdata.containsKey("sts") ? "" : jsdata.getString("sts");
		JSONObject jsobject = new JSONObject();
		jsobject.put("respCode", "-1");
		jsobject.put("respDesc", "数据异常");
	
		String sql="UPDATE  "+
					" SCHEDULE  "+
					" SET  "+
					"  sts = ?, "+
					"  sts_date = NOW() "+
					" WHERE id = '"+id+"' ";
		ArrayList<JavaModel> list = new ArrayList<>();		
		list.add(new JavaModel(sts));
		boolean flag=baseDAO.excuteSql(sql, list);		
		if(flag)
		{
			jsobject.put("respCode", "0");
			jsobject.put("respDesc", "success");
		}
		return jsobject;
	}
	
	/**
	 * 执行任务
	 * @param jsdata
	 * @return
	 */
	@Async
	public void scheduleExcute(JSONObject jsdata)
	{
		String id=!jsdata.containsKey("id") ? "" : jsdata.getString("id");
		String url=!jsdata.containsKey("url") ? "" : jsdata.getString("url");
		String req_data=!jsdata.containsKey("req_data") ? "" : jsdata.getString("req_data");
		JSONObject jsobject = new JSONObject();
		jsobject.put("respCode", "-1");
		jsobject.put("respDesc", "数据异常");	
//		url="http://12.1.1.43:8001//kpnmrb/test/autonkhd.mvc";
//		url="http://127.0.0.1:8080//ssm/pub/schedule/testUrl.mvc";
//		req_data="start_date=2020-05-05&end_date=2020-05-06";
		jsdata.put("id", id);
		jsdata.put("sts", "D");
		jsobject = scheduleUpateStatus(jsdata);		
			
		try {
			 System.out.println("异步任务执行开始");
				HashMap<String, String> headers = new HashMap<String, String>();
			    headers = new HashMap<String, String>();		  
				headers.put("Accept", "*/*"); //*/*
				headers.put("Content-Type", " application/x-www-form-urlencoded;charset=UTF-8");			
				ToolsUtil.postHttps(url, req_data, headers) ;
			    System.out.println("异步任务执行结束");
        } catch (Exception e) {
        	jsdata.put("id", id);
    		jsdata.put("sts", "Z");
    		jsobject = scheduleUpateStatus(jsdata);		
    		jsobject.put("respCode", "-1");
    		jsobject.put("respDesc", "fail");
        }
		jsdata.put("id", id);
		jsdata.put("sts", "F");
		jsobject = scheduleUpateStatus(jsdata);		
		jsobject.put("respCode", "0");
		jsobject.put("respDesc", "success");
	}
	
}
